﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace TVC.Code.cs
{
    public class clsCommon
    {
        public string CheckDate(string strDateInput)
        {
            try
            {
                if (strDateInput == "")
                    return strDateInput;

                string checkstr = "0123456789";
                string DateField = strDateInput;
                string Datevalue = "";
                string DateTemp = "";
                string seperator = "/";
                string day;
                string month;
                string year;
                int leap = 0;
                int err = 0;
                int i;
                err = 0;
                Datevalue = strDateInput.Replace('-', '/');
                Datevalue = Datevalue.Replace('.', '/');
                if (Datevalue.IndexOf('/') == 1)
                    Datevalue = "0" + Datevalue;
                if (Datevalue.LastIndexOf('/') == 4)
                {
                    Datevalue = Datevalue.Substring(0, 3) + "0" + Datevalue.Substring(3, Datevalue.Length - 3);
                }
                /* Delete all chars except 0..9 */
                for (i = 0; i < Datevalue.Length; i++)
                {
                    if (checkstr.IndexOf(Datevalue.Substring(i, 1)) >= 0)
                    {
                        DateTemp = DateTemp + Datevalue.Substring(i, 1);
                    }
                }
                Datevalue = DateTemp;
                /* Always change date to 8 digits - string*/
                /* if year is entered as 2-digit / always assume 20xx 
                HungDD edit : if year is entered as 2-digit / if 2 digits of year =< 70 assume 20xx / else assume 19xx
                */
                if (Datevalue.Length == 6)
                {
                    string str = Datevalue.Substring(4, 2);
                    //alert("str : " + str);
                    int y = 0;
                    if (str == "08") y = 8;
                    else if (str == "09") y = 9;
                    else
                        y = int.Parse(str);
                    //alert("y : " + y);

                    string strYear = y < 10 ? "0" + y : "" + y;

                    //alert("strYear : " + strYear);
                    if (y > 70)
                        Datevalue = Datevalue.Substring(0, 4) + "19" + strYear;
                    else
                        Datevalue = Datevalue.Substring(0, 4) + "20" + strYear;
                }

                if (Datevalue.Length != 8)
                {
                    //hungdd edit 05/05/2009
                    /*replace '/' & '-' & '.' to '/' */
                    string strDate = strDateInput;
                    DateTime currentDate = new DateTime();
                    string reg = @"/\d/";
                    string pattern = @"/\/{1,}/g";
                    if (strDate.IndexOf(".") > -1 || strDate.IndexOf("-") > -1)
                    {
                        strDate = strDate.Replace(@"/\./g", "/");
                        strDate = strDate.Replace("/-/g", "/");
                        strDate = strDate.Replace(pattern, "/");
                        string sDate = strDate.Substring(0, strDate.IndexOf("/"));
                        strDate = strDate.Replace(sDate + "/", "");
                        string sMonth = strDate.Substring(0, strDate.IndexOf("/"));
                        strDate = strDate.Replace(sMonth + "/", "");
                        string sYear = strDate.Substring(strDate.Length - 2, 2);//strDate);
                        if (sMonth == null || sMonth == "")
                        {
                            sMonth = sYear;
                            sYear = currentDate.Year.ToString();
                        }
                        //alert(sDate + ";" + sMonth + ";" + sYear);
                        if (Regex.IsMatch(sDate, reg) && Regex.IsMatch(sMonth, reg) && Regex.IsMatch(sYear, reg))
                        {
                            /*Compile to DateTime string*/
                            sDate = sDate.Length == 1 ? "0" + sDate : sDate;
                            sMonth = sMonth.Length == 1 ? "0" + sMonth : sMonth;
                            int intYear = int.Parse(sYear);
                            switch (sYear.Length)
                            {
                                case 1:
                                    sYear = "200" + sYear;
                                    break;
                                case 2:
                                    sYear = intYear <= 70 ? "20" + sYear : "19" + sYear;
                                    break;
                            }
                            //alert(date + ";" + month + ";" + year);
                            Datevalue = sDate + sMonth + sYear;
                        }
                        else
                            err = 19;
                    }
                    //end : hungdd edit 05/05/2009
                    else
                    {
                        if (Regex.IsMatch(strDate, reg))
                        {
                            int intDate = int.Parse(strDate);
                            DateTime myDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
                            //myDate.setFullYear(currentDate.getFullYear(), currentDate.getMonth() + 1, 1);
                            myDate = myDate.AddDays(-1);
                            //myDate.setDate(myDate.getDate() - 1);

                            if (intDate > myDate.Day || intDate <= 0)
                                err = 25;
                            else
                            {
                                Datevalue = intDate < 10 ? "0" + strDate : strDate;
                                int month_ = currentDate.Month + 1;
                                Datevalue += month_ < 10 ? "0" + month_ : "" + month_;
                                //Datevalue += currentDate.getFullYear();
                                Datevalue += currentDate.Year;
                            }
                        }
                        else
                            err = 19;
                    }
                }
                //end HungDD edit 
                /* year is wrong if year = 0000 */
                year = (Datevalue.Substring(4, 4));
                if (int.Parse(year) == 0)
                {
                    err = 20;
                }
                /* Validation of month*/
                month = (Datevalue.Substring(2, 2));
                if ((int.Parse(month) < 1) || (int.Parse(month) > 12))
                {
                    err = 21;
                }
                /* Validation of day*/
                day = (Datevalue.Substring(0, 2));

                if (int.Parse(year) <= 1900)
                {
                    err = 18;
                }
                if (int.Parse(day) < 1)
                {
                    err = 22;
                }
                /* Validation leap-year / february / day */
                if ((int.Parse(year) % 4 == 0) || (int.Parse(year) % 100 == 0) || (int.Parse(year) % 400 == 0))
                {
                    leap = 1;
                }
                if ((int.Parse(month) == 2) && (leap == 1) && (int.Parse(day) > 29))
                {
                    err = 23;
                }
                if ((int.Parse(month) == 2) && (leap != 1) && (int.Parse(day) > 28))
                {
                    err = 24;
                }
                /* Validation of other int.Parse(month)s */
                if ((int.Parse(day) > 31) && ((int.Parse(month) == 01) || (int.Parse(month) == 03) || (int.Parse(month) == 05) || (int.Parse(month) == 07) || (int.Parse(month) == 08) || (int.Parse(month) == 10) || (int.Parse(month) == 12)))
                {
                    err = 25;
                }
                if ((int.Parse(day) > 30) && ((int.Parse(month) == 04) || (int.Parse(month) == 06) || (int.Parse(month) == 09) || (int.Parse(month) == 11)))
                {
                    err = 26;
                }
                /* if 00 ist entered, no error, deleting the entry */
                if ((int.Parse(day) == 0) && (int.Parse(month) == 0) && (int.Parse(year) == 00))
                {
                    err = 0; day = ""; month = ""; year = ""; seperator = "";
                }
                /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
                if (err == 0)
                {
                    DateField = day + seperator + month + seperator + year;
                    //DateTime dtToDate = DateTime.ParseExact(DateField, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);
                    strDateInput = DateField;
                    //dtpCustom.Value = dtToDate;
                    return strDateInput;// true;
                }
                else if (err == 18)/* Error-message if err != 0 */
                {
                    strDateInput = "";
                    //txtDate.Focus();
                    //GetAlertError(iTotal, DSAlert, "0030");
                    return strDateInput;// false;
                }
                else
                {
                    strDateInput = "";
                    //txtDate.Focus();
                    //GetAlertError(iTotal, DSAlert, "0031");
                    return strDateInput;// false;
                }
            }
            catch (Exception ex)
            {
                strDateInput = "";
                //txtDate.Focus();
            }
            return strDateInput;
        }

    }
}